#include "dbs.h"
#include "ui_dbs.h"
#include <QPainter>
extern LinkList<Node>stu;

DBS::DBS(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DBS)
{
    ui->setupUi(this);
    setWindowTitle("学生信息库");
    this->model = new QStandardItemModel;
    this->ui->tableView->setModel(model);
    model->setColumnCount(6);
    model->setHeaderData(0,Qt::Horizontal,"学号");
    model->setHeaderData(1,Qt::Horizontal,"姓名");
    model->setHeaderData(2,Qt::Horizontal,"性别");
    model->setHeaderData(3,Qt::Horizontal,"院系");
    model->setHeaderData(4,Qt::Horizontal,"宿舍号");
    model->setHeaderData(5,Qt::Horizontal,"班级");
    ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
    this->ui->tableView->setColumnWidth(0,100);
    this->ui->tableView->setColumnWidth(1,100);
    this->ui->tableView->setColumnWidth(2,60);
    this->ui->tableView->setColumnWidth(3,200);
    this->ui->tableView->setColumnWidth(4,60);
    this->ui->tableView->setColumnWidth(5,150);
    for(int i = 0; i < stu.GetLength(); i++)
     {
        QString id,name,sex,faculty,roomnu,classnu;
        Node a;
        stu.GetData(i+1,a);
        id=a.Number;
        name=a.Name;
        sex=a.Gender;
        faculty=a.Faculty;
        roomnu=a.RoomNu;
        classnu=a.ClassNu;
         model->setItem(i,0,new QStandardItem(id));
         model->setItem(i,1,new QStandardItem(name));
         model->setItem(i,2,new QStandardItem(sex));
         model->setItem(i,3,new QStandardItem(faculty));
         model->setItem(i,4,new QStandardItem(roomnu));
         model->setItem(i,5,new QStandardItem(classnu));
     }

}

DBS::~DBS()
{
    delete ui;
}

void DBS::on_dbs_return_clicked()
{
    mainWindow*mWin=new mainWindow;
    mWin->show();
    delete this;
}

void DBS::on_querySearch_clicked()
{
    QString s=ui->queryWay->currentText();
    QString t=ui->queryLine->text();
    if(s=="按学号查询")
    {
        int a=stu.FindId(t);
        if(a==0)
            QMessageBox::warning(this,"警告","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;

            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.Number!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }

            }


        }
    }
    if(s=="按姓名查询")
    {
        int a=stu.FindName(t);
        if(a==0)
            QMessageBox::warning(this,"","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;
            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.Name!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }
            }

        }
    }
    if(s=="按院系查询")
    {
        int a=stu.FindF(t);
        if(a==0)
            QMessageBox::warning(this,"","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;
            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.Faculty!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }
            }

        }
    }
    if(s=="按宿舍号查询")
    {
        int a=stu.FindR(t);
        if(a==0)
            QMessageBox::warning(this,"","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;
            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.RoomNu!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }
            }

        }
    }
    if(s=="按性别查询")
    {
        int a=stu.Find_Sex(t);
        if(a==0)
            QMessageBox::warning(this,"","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;
            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.Gender!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }
            }

        }
    }
    if(s=="按班级查询")
    {
        int a=stu.Find_ClassName(t);
        if(a==0)
            QMessageBox::warning(this,"","无此学生");
        else
        {
            model->clear();
            model->setColumnCount(6);
            model->setHeaderData(0,Qt::Horizontal,"学号");
            model->setHeaderData(1,Qt::Horizontal,"姓名");
            model->setHeaderData(2,Qt::Horizontal,"性别");
            model->setHeaderData(3,Qt::Horizontal,"院系");
            model->setHeaderData(4,Qt::Horizontal,"宿舍号");
            model->setHeaderData(5,Qt::Horizontal,"班级");
            ui->tableView->horizontalHeader()->setDefaultAlignment(Qt::AlignJustify);
            this->ui->tableView->setColumnWidth(0,100);
            this->ui->tableView->setColumnWidth(1,100);
            this->ui->tableView->setColumnWidth(2,60);
            this->ui->tableView->setColumnWidth(3,200);
            this->ui->tableView->setColumnWidth(4,60);
            this->ui->tableView->setColumnWidth(5,150);
            LinkNode<Node>*p=stu.head->next;
            int i=0;
            while(p!=NULL)
            {
                while(p!=NULL&&(p->data.ClassNu!=t))
                    p=p->next;
                if(p!=NULL)
                {
                   model->setItem(i,0,new QStandardItem(p->data.Number));
                   model->setItem(i,1,new QStandardItem(p->data.Name));
                   model->setItem(i,2,new QStandardItem(p->data.Gender));
                   model->setItem(i,3,new QStandardItem(p->data.Faculty));
                   model->setItem(i,4,new QStandardItem(p->data.RoomNu));
                   model->setItem(i,5,new QStandardItem(p->data.ClassNu));
                   i++;
                   p=p->next;
                }
            }

        }
    }
}

void DBS::paintEvent(QPaintEvent *)
{
   QPainter p(this);//创建画家对象p
    //绘图操作
    p.drawPixmap(rect(),QPixmap(":/new/prefix1/beffc781gy1gtid8puiz5j20zk0qoad0.jpg"));

}
